package fr.licpro.filebox.model;

import java.util.Date;

import android.os.Parcel;
import android.os.Parcelable;

import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import fr.licpro.filebox.dto.commons.FileDto;
import fr.licpro.filebox.dto.enums.FileTypeEnum;

/**
 * File present in the server or in BDD.
 * @author Abdessamad EL FATTAKE
 * @author Sylvain LE GOUELLEC
 * @version 1.0.0
 * @since 1.0.0
 */
@DatabaseTable(tableName="file")
public class File implements Parcelable
{
	/**
	 *	File's ID
	 */
	@DatabaseField(columnName="ID", id=true)
	private String mHashId;
	
	/**
	 *	File's name
	 */
	@DatabaseField(columnName="NAME", canBeNull=false)
	private String mName;
	
	/**
	 *	File's type
	 */
	@DatabaseField(columnName="FILE_TYPE",canBeNull=true,dataType=DataType.ENUM_STRING)
	private FileTypeEnum mFileType;
	
	/**
	 *	LastModification of file
	 */
	@DatabaseField(columnName="LAST_UPDATE",canBeNull=false,dataType=DataType.DATE)
	private Date mLastModification;
		
	/**
	 *	Parent's file
	 */
	@DatabaseField(foreign=true,foreignAutoRefresh=true,foreignColumnName="ID",foreignAutoCreate=true,columnName="PARENT_FOLDER")
	private File mParentFolder;
	
	/**
	 * Default constructor
	 */
	public File(){
	}
	
	/**
	 * Constructor for convert a FileDTO in File.
	 * @param file	File to convert
	 * @param parent	Parent's file 
	 */
	public File(FileDto file, File parent)  
	{
		mHashId = file.getHashId();
		mName = file.getName();
		mFileType = file.getFileType();
		mLastModification = file.getLastModification();
		mParentFolder = parent;
	}
	
	/**
	 * Constructor for convert a FileDTO in File without parent File.
	 * @param file	File to convert
	 */
	public File(FileDto file)  
	{
		this(file, null);
	}
	
	/* (non-Javadoc)
	 * @see android.os.Parcelable#describeContents()
	 */
	@Override
	public int describeContents() {
		return 0;
	}

	/* (non-Javadoc)
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(mHashId);
		dest.writeString(mName);
		dest.writeLong(mLastModification == null ? 0 : mLastModification.getTime());
				
		dest.writeParcelable(mParentFolder, 0);
				
		String s = mFileType == null ? "FOLDER" : "FILE";
		dest.writeString(s);
		// C'est un fichier
		if(mFileType != null)
			dest.writeInt(mFileType.ordinal());

	}
	
	/**
	 * Parcelable Creator
	 */
	public static final Parcelable.Creator<File> CREATOR
	    = new Parcelable.Creator<File>() {
		public File createFromParcel(Parcel in) {
		    return new File(in);
		}
		
		public File[] newArray(int size) {
		    return new File[size];
		}
	};
	
    /**
     * Constructor for Parcelable implementation
     * @param in 	Instance of object serialized
     */
	private File(Parcel in) {
		mHashId = in.readString();
		mName = in.readString();
		mLastModification = new Date(in.readLong());
		
		mParentFolder = in.readParcelable(File.class.getClassLoader());
		
		String type = in.readString();
		if(type.equals("FILE")){
			mFileType = FileTypeEnum.values()[in.readInt()];
		}
	}

	/********************************************************/
	/**						GETTER / SETTER 			   **/
	/********************************************************/
	
	public String getHashId() {
		return mHashId;
	}

	public void setHashId(String hashId) {
		this.mHashId = hashId;
	}

	public String getName() {
		return mName;
	}

	public void setName(String name) {
		this.mName = name;
	}

	public FileTypeEnum getFileType() {
		return mFileType;
	}

	public void setFileType(FileTypeEnum fileType) {
		this.mFileType = fileType;
	}

	public Date getLastModification() {
		return mLastModification;
	}

	public void setLastModification(Date lastModification) {
		this.mLastModification = lastModification;
	}

	public File getParentFolder() {
		return mParentFolder;
	}

	public void setParentFolder(File parentFolder) {
		this.mParentFolder = parentFolder;
	}
}
